<?php

namespace app\admin\controller\shop;

use app\admin\model\ShopCate;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="shop_cate")
 */
class Cate extends AdminController
{

    use Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = ShopCate::init();
        
    }

    protected $allowModifyFields = [
        'title', 'brief', 'sort', 'status'
    ];

    protected $sort = [
        'sort' => 'desc',
        'id' => 'asc',
    ];

    /**
     * @NodeAnotation(title="添加")
     * @param mixed $id
     * @return mixed
     */
    public function add($id = null)
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'pid|上级分类' => 'require',
                'title|分类名称' => 'require|unique:shopCate',
//                'icon|分类图标' => 'require',
            ];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败，'.$e->getMessage());
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $cate_list = $this->model->getCateList();
        $this->assign([
            'id' => $id,
            'cate_list' => $cate_list
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'pid|上级分类' => 'require',
                'title|分类名称' => 'require|unique:shopCate',
//                'icon|分类图标' => 'require',
            ];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败，'.$e->getMessage());
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $cate_list = $this->model->getCateList();
        $this->assign([
            'id' => $id,
            'row' => $row,
            'cate_list' => $cate_list
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="属性修改")
     */
    public function modify()
    {
        $post = $this->request->post();
        $rule = [
            'id|ID' => 'require',
            'field|字段' => 'require',
            'value|值' => 'require',
        ];
        $this->validate($post, $rule);
        $row = $this->model->find($post['id']);
        if (!$row) {
            $this->error('数据不存在');
        }
        if (!in_array($post['field'], $this->allowModifyFields)) {
            $this->error('该字段不允许修改：' . $post['field']);
        }
        //名称去重
        if ($post['field'] == 'title') {
            if ($this->model->where('title', $post['value'])->find()) {
                $this->error('该分类名已存在');
            }
        }
        try {
            $row->save([
                $post['field'] => $post['value'],
            ]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }


}